package ua.org.enginer.controls
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	public class HelpBuilder
	{
		public var controlPanel:HelpBuilderControlPanel
		
		private var tips:Array = []
		
		public function HelpBuilder()
		{
			super();
			
			controlPanel = new HelpBuilderControlPanel()
			PopUpManager.addPopUp(controlPanel, Application.application as DisplayObject)
			PopUpManager.centerPopUp(controlPanel)
			
			controlPanel.addEventListener('addHelpTip', addHelpTip)
			controlPanel.addEventListener('dump', getDump)
		}
		
		private function addHelpTip(event:Event):void {
			var tip:HelpTip = new HelpTip()
			tip.htmlText = controlPanel.editor.text
			tip.data = controlPanel.clazz.text
			PopUpManager.addPopUp(tip, Application.application as DisplayObject, false, "helpTips")
			
			tips.push(tip)
		}
		
		public function getDump(event:Event):void {
			Alert.show(dump().toXMLString())
		}
		
		private function dump():XML {
			var tip:HelpTip
			var result:XML = <root/>
			var tipXml:XML
			
			for (var i:int; i<tips.length; i++) {
					tip = HelpTip(tips[i])
					tipXml = <tip>{tip.htmlText}</tip>
					tipXml.@x = tip.x
					tipXml.@y = tip.y
					tipXml.@clazz = tip.data
					result.appendChild(tipXml)
			}
			
			return result
		}
	}
}